package client.popups.wizard;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.awt.Dimension;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JList;

import miscellaneous.Storage;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import data.PropertyChangeSpeaker;
import data.User;

public class MeetingParticipantsWizard extends JPanel implements PropertyChangeSpeaker {
	private JTextField searchField;
	private JButton addButton;
	private JButton removeButton;
	private JButton finishButton;
	private JList participantsJList;
	private JList employeesJList;
	private DefaultListModel participantsModel;
	private DefaultListModel employeesModel;
	private ArrayList<User> employeeList;
	private List<User> participantsList;
	
	private PropertyChangeSupport pcs;
	/**
	 * Create the panel.
	 */
	public MeetingParticipantsWizard(List<User> participantsOverview, boolean isWizard) {
		if(!isWizard){
			pcs = new PropertyChangeSupport(this);
		}
		employeeList = Storage.users.getCopyOfList();
		employeeList.remove(Storage.getloggedInUser()); //Removes the user logged in from the searchfield
		participantsList = new ArrayList<User>();
		
		employeesModel = new DefaultListModel();
		participantsModel = new DefaultListModel();
		
		//add users from storage userList
		//add users from the list of participants sent by participantsOverview and removes them for the employeesModel
		if(participantsOverview != null){
			for (User user : employeeList) {
				if(!participantsOverview.contains(user)){
					employeesModel.addElement(user);
				}
			}
			for (User user : participantsOverview) {
				participantsModel.addElement(user);
				participantsList.add(user);
				employeeList.remove(user);
			}
		} else {
			for (User user : employeeList) {
				employeesModel.addElement(user);
			}
		}
		
		this.setSize(325, 241);
		this.setPreferredSize(new Dimension(431, 431));
		this.setMaximumSize(new Dimension(325,241));
		JLabel participants = new JLabel("Deltakere:");
		participants.setBounds(10, 4, 92, 14);
		
		JLabel search = new JLabel("S\u00F8k:");
		search.setBounds(10, 23, 33, 14);
		
		searchField = new JTextField();
		searchField.setBounds(41, 24, 162, 25);
		searchField.setColumns(10);
		searchField.addKeyListener(new SearchKeyListener());
		
		addButton = new JButton("Legg til");
		addButton.setBounds(74, 353, 92, 23);
			addButton.addActionListener(new ButtonActionListener());
		
		JLabel employees = new JLabel("Ansatte:");
		employees.setBounds(10, 50, 74, 14);
		
		employeesJList = new JList(employeesModel);
		
		employeesJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		JScrollPane employeeScroll = new JScrollPane(employeesJList);
		employeeScroll.setBounds(30, 70, 179, 272);
		
		JLabel participantLabel = new JLabel("Deltakere:");
		participantLabel.setBounds(219, 50, 86, 14);
		
		JScrollPane participantsScroll = new JScrollPane();
		participantsScroll.setBounds(224, 70, 179, 272);
		
		
		removeButton = new JButton("Fjern");
		removeButton.setBounds(270, 353, 92, 23);
			removeButton.addActionListener(new ButtonActionListener());
		setLayout(null);
		add(search);
		add(searchField);
		add(participantsScroll);
		participantsJList = new JList(participantsModel);
		participantsJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		participantsScroll.setViewportView(participantsJList);
		add(employeeScroll);
		add(removeButton);
		add(addButton);
		add(participants);
		add(employees);
		add(participantLabel);
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		separator.setBounds(216, 70, 11, 272);
		add(separator);
		
		if(!isWizard){
			finishButton = new JButton("Ferdig");
			finishButton.setBounds(314, 397, 89, 23);
			finishButton.addActionListener(new ButtonActionListener());
			add(finishButton);
		}

	}
	public JTextField getSearchField() {
		return searchField;
	}
	public JButton getAddButton() {
		return addButton;
	}
	
	public List<User> getParticipantsList() {
		return participantsList;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener){
		if(pcs != null){
			addPCL(listener);
		}
	}

	public void removePropertyChangeListener(PropertyChangeListener listener){
		if(pcs != null){
			removePCL(listener);
		}
	}
	public void addPCL(PropertyChangeListener l) { 
  		pcs.addPropertyChangeListener(l);  
	}

	public void removePCL(PropertyChangeListener l) { 
  		pcs.removePropertyChangeListener(l);  
	}
	public class ButtonActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource() == addButton){
				Object ue[] = employeesJList.getSelectedValues();
				if(ue != null){
					for (int i = 0; i < ue.length; i++) {
						User u = (User) ue[i];
						participantsModel.addElement(u);
						employeesModel.removeElement(u);
						employeeList.remove(u);
						participantsList.add(u);
					}
				}
			} else if(e.getSource() == removeButton){
				Object up[] = participantsJList.getSelectedValues();
				if(up != null){
					for (int i = 0; i < up.length; i++) {
						User u = (User)up[i];
						employeesModel.addElement(u);
						participantsModel.removeElement(u);
						employeeList.add(u);
						participantsList.remove(u);
					}
					Collections.sort(employeeList);
				}
			} else if(e.getSource() == finishButton){
				pcs.firePropertyChange("sendParticipantsList", null, participantsList);
			}
		}
		
	}
	
	public class SearchKeyListener implements KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			employeesModel.clear();
			String searchtText = searchField.getText();
			int searchLength = searchtText.length();
			
			if(searchtText.equals("")){
				for (User user : employeeList) {
					employeesModel.addElement(user);
				}
			}else {
				for (User user : employeeList) {
					int userNameLength = user.getName().length();
					if(userNameLength < searchLength) {
						continue;
					}else if(user.getName().substring(0, searchLength).equalsIgnoreCase(searchtText)){
					employeesModel.addElement(user);
					
					}
				}
			}
			employeesJList.validate();
			employeesJList.updateUI();
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
